<?php

class Authenticate extends Controller {

	function __construct()
	{
		parent::Controller();
		$this->load->model('user');	
	}
	
	function index()
	{
		$this->login();
	}
	
	function login()
	{ 
		
		if ($this->auth->check('only')) {
			
			if($this->auth->check('level',4)) {
			 	redirect('admin');
			} else {
				redirect(); 
			}
			
		} 
		else 
		{

			$this->form_validation->set_rules('username', 'Username', 'required');
			$this->form_validation->set_rules('password', 'Password', 'required');

			if ($this->form_validation->run() == FALSE)
			{
				$data['error'] = '';
				$this->oryx->render('login', $data);
			}
			else
			{
				$display_name = $this->input->post('username');
				$password = $this->input->post('password');

				if ($this->auth->login($display_name, $password)) {
					
					if($this->auth->check('level',4)) {
					 	redirect('admin');
					} else {
						redirect(); 
					}
					
				} else {
					$data['error'] = '<p style="color:red;">Invalid username or password.</p>'; 
					
					$this->oryx->render('login',$data);
				}
			}
		
		}
	
	}
	
	function logout()
	{ 
		$this->auth->logout();
		$data['body'] = 'logout'; 	
		$this->oryx->render('single_page',$data);
	}

}
	